<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>

  <body>
    <script>
      var promise = new Promise(function (resolve, reject) {
        var ajax = new XMLHttpRequest();
        ajax.open('get', 'https://cnodejs.org/api/v1/topics', true);
        ajax.onreadystatechange = function () {
          if (ajax.readyState == 4) {
            //数据接收成功
            if (ajax.status == 200) {
              //返回数据正常
              var data = JSON.parse(ajax.responseText); //json数据解析为js对象
              resolve(data);
            } else {
              //返回数据不正常
              // console.dir(ajax)
              reject(new Error('Request was unsuccessful: ' + ajax.statusText));
            }
          }
        };
        ajax.send(null);
      });
      promise
        .then(function (data) {
          console.log("data:",data);
        })
        .catch(function (err) {
          console.log("err:",err);
        })
        .finally();
    </script>
  </body>
</html>
